﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BFCOMAssistant
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //定义一个串口对象
        private ComPort comPort = new ComPort();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //查询串口
            string[] strArr = System.IO.Ports.SerialPort.GetPortNames();
            for (int i = 0; i < strArr.Length; i++)
            {
                cbComName.Items.Add(strArr[i]);
            }
            if (cbComName.Items.Count > 0)
            {
                cbComName.Text = cbComName.Items[0].ToString();

                comPort.Parameter.Name = cbComName.Text;
                comPort.Parameter.BaudRate = Convert.ToInt32(cbComBaudRate.Text);
                comPort.Parameter.DataBit = Convert.ToInt32(cbComDataBits.Text);
                comPort.Parameter.StopBit = Convert.ToInt32(cbComStopBits.Text);
                comPort.Parameter.Parity = cbComParity.Text;
            }

            cbComClose.IsEnabled = false;

            //绑定事件
            comPort.ReadComDataEvent += new ReadComDataDelegate(comPort_ReadData);
        }

        private void MenuExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnTBData_Click(object sender, RoutedEventArgs e)
        {
            Button btnTextBoxData = sender as Button;
            string result = "";
            byte[] dispData = new byte[1];
            string SendDataFormat = "HEX";
            string DispalyFormat = "HEX";
            string DispalySendDataHead = "Sent: ";
            string DispalySendDataBody = "";

            if (rbSendDataByASCII.IsChecked == true)
            {
                SendDataFormat = "ASCII";
            }

            if(rbDisplyByASCII.IsChecked == true)
            {
                DispalyFormat = "ASCII";
            }

            switch (btnTextBoxData.Name)
            {
                case "btnDataSend1Send":
                    result = comPort.Write(tbDataSend1.Text, SendDataFormat, out dispData);
                    break;
                case "btnDataSend2Send":
                    result = comPort.Write(tbDataSend2.Text, SendDataFormat, out dispData);
                    break;
                case "btnDataSend3Send":
                    MessageBox.Show("发送成功3");
                    break;
                case "btnDataSend4Send":
                    MessageBox.Show("发送成功4");
                    break;
                case "btnDataSend5Send":
                    MessageBox.Show("发送成功5");
                    break;
                case "btnDataSend1Clear":
                    tbDataSend1.Text = "";
                    break;
                case "btnDataSend2Clear":
                    tbDataSend2.Text = "";
                    break;
                case "btnDataSend3Clear":
                    tbDataSend3.Text = "";
                    break;
                case "btnDataSend4Clear":
                    tbDataSend4.Text = "";
                    break;
                case "btnDataSend5Clear":
                    tbDataSend5.Text = "";
                    break;
                default:
                    break;
            }

            //打印显示数据
            if (result != "OK")
            {
                MessageBox.Show(result, "Waring", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                if (DispalyFormat == "HEX")
                {
                    for (int i = 0; i < dispData.Length; i++)
                    {
                        DispalySendDataBody += dispData[i].ToString("X2") + " ";
                    }
                }
                else //ASCII
                {
                    BasicDataFormatLib.HexToASCII(dispData, out DispalySendDataBody);
                    
                }
                tbDataRecevie.AppendText(DispalySendDataHead + DispalySendDataBody + "\n");
                tbDataRecevie.ScrollToEnd();
            }
        }

        private void cbCom_DropDownClosed(object sender, EventArgs e)
        {
            if (cbComName.Text != "")
            {
                comPort.Parameter.Name = cbComName.Text;
                comPort.Parameter.BaudRate = Convert.ToInt32(cbComBaudRate.Text);
                comPort.Parameter.DataBit = Convert.ToInt32(cbComDataBits.Text);
                comPort.Parameter.StopBit = Convert.ToInt32(cbComStopBits.Text);
                comPort.Parameter.Parity = cbComParity.Text;
            }
        }

        private void cbComNameRefresh_Click(object sender, RoutedEventArgs e)
        {
            //查询串口

            cbComName.Items.Clear();

            string[] strArr = System.IO.Ports.SerialPort.GetPortNames();
            for (int i = 0; i < strArr.Length; i++)
            {
                cbComName.Items.Add(strArr[i]);
            }
            if (cbComName.Items.Count > 0)
            {
                cbComName.Text = cbComName.Items[0].ToString();

                comPort.Parameter.Name = cbComName.Text;
                comPort.Parameter.BaudRate = Convert.ToInt32(cbComBaudRate.Text);
                comPort.Parameter.DataBit = Convert.ToInt32(cbComDataBits.Text);
                comPort.Parameter.StopBit = Convert.ToInt32(cbComStopBits.Text);
                comPort.Parameter.Parity = cbComParity.Text;
            }
        }

        private void cbComOpen_Click(object sender, RoutedEventArgs e)
        {
            string result;
            result = comPort.Open(comPort.Parameter);
            if (result != "OK")
            {
                MessageBox.Show(result, "Waring", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                cbComClose.IsEnabled = true;
                cbComOpen.IsEnabled = false;
                cbComNameRefresh.IsEnabled = false;
            } 
        }

        private void cbComClose_Click(object sender, RoutedEventArgs e)
        {
            string result;
            result = comPort.Close();
            if (result != "OK")
            {
                MessageBox.Show(result, "Waring", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                cbComClose.IsEnabled = false;
                cbComOpen.IsEnabled = true;
                cbComNameRefresh.IsEnabled = true;
            }
        }

        private void comPort_ReadData(byte[] dispData, Int32 Len)
        {
            string DispalyReceiveDataHead = "Received: ";
            string DispalyReceiveDataBody = "";
            string DispalyFormat = "HEX";

            this.Dispatcher.Invoke(new Action(() =>
            {
                if (rbDisplyByASCII.IsChecked == true)
                {
                    DispalyFormat = "ASCII";
                }

                if (DispalyFormat == "HEX")
                {
                    for (int i = 0; i < Len; i++)
                    {
                        DispalyReceiveDataBody += dispData[i].ToString("X2") + " ";
                    }
                }
                else //ASCII
                {
                    byte[] temp = new byte[Len];
                    for (int i = 0; i < temp.Length; i++)
                    {
                        temp[i] = dispData[i];
                    }
                    BasicDataFormatLib.HexToASCII(temp, out DispalyReceiveDataBody);
                }

                tbDataRecevie.AppendText(DispalyReceiveDataHead + DispalyReceiveDataBody + "\n");
                tbDataRecevie.ScrollToEnd();
            }));
            
        }

        private void btnClearDisplyData_Click(object sender, RoutedEventArgs e)
        {
            tbDataRecevie.Text = "";
        }
    }
}
